package com.huazi.servicebase.exceptionhandler;

import com.huazi.commonutils.CommentResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author shkstart
 * @create 2021-03-12 18:39
 */
@ControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)//指出接收哪些异常
    @ResponseBody//为了能够返回数据
    public CommentResult error(Exception e){
        System.out.println(e.getMessage());
        return CommentResult.getError().setMsg("执行了全局异常！").data("message",e.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)//指出接收哪些异常
    @ResponseBody//为了能够返回数据
    public CommentResult error(ArithmeticException e){
        System.out.println(e.getMessage());
        return CommentResult.getError().setMsg("执行了指定异常！").data("message",e.getMessage());
    }

    //自定义异常
    @ExceptionHandler(MyException.class)//指出接收哪些异常
    @ResponseBody//为了能够返回数据
    public CommentResult error(MyException e){
        System.out.println(e.getMessage());
        return CommentResult.getError().setMsg("执行了自定义异常！").data("message",e.getMessage());
    }
}
